package com.dormitory.activity;

import android.os.AsyncTask;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.dormitory.R;
import com.dormitory.controller.UserController;

public class RegisterActivity extends AppCompatActivity {


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);
        ((Button)findViewById(R.id.button)).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String name = ((TextView)findViewById(R.id.editUsername)).getText().toString();
                String password = ((TextView)findViewById(R.id.editPassword)).getText().toString();
                String confirmPassword = ((TextView)findViewById(R.id.editPassword2)).getText().toString();
                String dormitory = ((TextView)findViewById(R.id.editDormitory)).getText().toString();
                if(name == "" || password == "" || confirmPassword == "") {
                    Toast.makeText(RegisterActivity.this, "用户名不能为空", Toast.LENGTH_SHORT).show();
                }
                else if(!password.equals(confirmPassword)) {
                    Toast.makeText(RegisterActivity.this, "两次密码输入不一致", Toast.LENGTH_SHORT).show();
                }
                else {

                    RegisterAsyncTask registerAsyncTask = new RegisterAsyncTask();
                    registerAsyncTask.execute(name, password, dormitory, "");
                    /*SharedPreferences sp = getApplicationContext().getSharedPreferences("login", MODE_PRIVATE);
                    SharedPreferences.Editor editor = sp.edit();
                    editor.putString("name", name);
                    editor.putString("password", password);
                    editor.commit();
                    finish();*/

                }
            }
        });
    }
    private class RegisterAsyncTask extends AsyncTask<String, Object, Integer> {

        @Override
        protected Integer doInBackground(String[] params) {
            UserController userController = UserController.getInstance();
            return userController.register(params[0], params[1],params[2], params[3]);
        }

        @Override
        protected void onPostExecute(Integer code) {
            super.onPostExecute(code);
            switch(code) {
                case 0:
                    finish();
                    break;
                case 1:
                    Toast.makeText(RegisterActivity.this, "用户名已存在", Toast.LENGTH_SHORT).show();
                    break;
                case 3:
                    Toast.makeText(RegisterActivity.this, "寝室不存在", Toast.LENGTH_SHORT).show();
                    break;
                default:
                    Toast.makeText(RegisterActivity.this, "网络错误", Toast.LENGTH_SHORT).show();
            }
        }
    }
}
